package com.atguigu.datastream.test.day06;

import com.atguigu.datastream.bean.WaterSensor;
import org.apache.flink.api.common.functions.MapFunction;
import org.apache.flink.api.java.tuple.Tuple;
import org.apache.flink.streaming.api.datastream.DataStreamSource;
import org.apache.flink.streaming.api.datastream.KeyedStream;
import org.apache.flink.streaming.api.datastream.SingleOutputStreamOperator;
import org.apache.flink.streaming.api.environment.StreamExecutionEnvironment;
import org.apache.flink.streaming.api.functions.KeyedProcessFunction;
import org.apache.flink.util.Collector;

/**
 * ClassName: Flink05_Flink_Timer_ProcessTime
 * Package: com.atguigu.test.day06
 * Description:
 *                 基于处理事时间的定时器
 * @Author ChenJun
 * @Create 2023/4/13 9:57
 * @Version 1.0
 */
public class Flink05_Flink_Timer_ProcessTime {
    public static void main(String[] args) throws Exception {

        //1. 创建流的执行环境
        StreamExecutionEnvironment env = StreamExecutionEnvironment.getExecutionEnvironment();

        env.setParallelism(1);

        //2. 从端口读取数据
        DataStreamSource<String> streamSource = env.socketTextStream("localhost", 9999);

        //3. 将数据转换为JavaBean
        SingleOutputStreamOperator<WaterSensor> waterSensorStream = streamSource.map(new MapFunction<String, WaterSensor>() {
            @Override
            public WaterSensor map(String value) throws Exception {
                String[] split = value.split(",");
                return new WaterSensor(split[0], Long.parseLong(split[1]), Integer.parseInt(split[2]));
            }
        });

        //4. 将相同的key聚合到一起
        KeyedStream<WaterSensor, Tuple> keyedStream = waterSensorStream.keyBy("id");

        //TODO 5.使用基于处理时间的定时器
        SingleOutputStreamOperator<String> streamOperator = keyedStream.process(new KeyedProcessFunction<Tuple, WaterSensor, String>() {
            @Override
            public void processElement(WaterSensor value, KeyedProcessFunction<Tuple, WaterSensor, String>.Context ctx, Collector<String> out) throws Exception {
                // TODO 定时时间为5s 5s后定时器触发打印一句话
                // 获取当前处理时间
//                System.out.println("事件时间为：" + (System.currentTimeMillis() / 1000));
//                long time = curTime + 5000;
                long curTime = ctx.timerService().currentProcessingTime();
                long time = curTime + 200;
                // 注册定时器
                ctx.timerService().registerProcessingTimeTimer(time);
            }

            /**
             * 回执方法
             * @param timestamp The timestamp of the firing timer.
             * @param ctx
             * @param out The collector for returning result values.
             * @throws Exception
             */
            @Override
            public void onTimer(long timestamp, KeyedProcessFunction<Tuple, WaterSensor, String>.OnTimerContext ctx, Collector<String> out) throws Exception {
//                out.collect("当前事件为：" + (System.currentTimeMillis() / 1000));
                ctx.timerService().registerProcessingTimeTimer(timestamp + 200);
                out.collect("WaterMark：" + (timestamp / 1000));

            }
        });

        streamOperator.print();

        env.execute();

    }
}
